package dobest.ray.com.jiongjiong.views;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver;

import dobest.ray.com.jiongjiong.R;

/**
 * Created by Ray on 2016/3/11.
 * https://github.com/ray0807
 */
public class ArcCountView extends View {
    private Paint paintWhite;
    private Paint paintMainColor;
    private Context context;
    private float tb;

    private RectF rectf;
    private RectF rectfMain;

    private int score;
    private float arc_y = 0f;

    private int blackColor = 0x70000000; // 底黑色
    private int whiteColor = 0xddffffff; // 白色

    public ArcCountView(Context context, int score) {
        super(context);
        this.context = context;
        this.score = score;
        init();
    }

    public ArcCountView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public ArcCountView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    private void init() {
        initPaint();
        initRect();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawArc(rectf, -90, 360, false, paintMainColor);
        canvas.drawArc(rectfMain, -90, arc_y, false, paintWhite);


        super.onDraw(canvas);
    }

    private void initPaint() {
        Resources res = getResources();
        tb = res.getDimension(R.dimen.historyscore_tb);


        paintMainColor = new Paint();
        paintMainColor.setAntiAlias(true);
        paintMainColor.setColor(blackColor);
        paintMainColor.setStrokeWidth(tb * 2f);
        paintMainColor.setStyle(Paint.Style.STROKE);

        paintWhite = new Paint();
        paintWhite.setAntiAlias(true);
        paintWhite.setColor(whiteColor);
        paintWhite.setStrokeWidth(tb * 2f);
        paintWhite.setStyle(Paint.Style.STROKE);
    }

    private void initRect() {
        rectf = new RectF();
        rectf.set(tb * 1.7f, tb * 1.7f, tb * 18.3f, tb * 18.3f);
        rectfMain = new RectF();
        rectfMain.set(tb * 1.4f, tb * 1.4f, tb * 18.6f, tb * 18.6f);

        setLayoutParams(new LayoutParams((int) (tb * 20.5f), (int) (tb * 20.5f)));

        this.getViewTreeObserver().addOnPreDrawListener(
                new ViewTreeObserver.OnPreDrawListener() {
                    public boolean onPreDraw() {
                        new CountThread();
                        getViewTreeObserver().removeOnPreDrawListener(this);
                        return false;
                    }
                });
    }


    class CountThread implements Runnable {
        private Thread thread;
        private int statek;
        int count;

        public CountThread() {
            thread = new Thread(this);
            thread.start();
        }

        public void run() {
            while (true) {
                switch (statek) {
                    case 0:
                        try {
                            Thread.sleep(200);
                            statek = 1;
                        } catch (InterruptedException e) {
                        }
                        break;
                    case 1:
                        try {
                            Thread.sleep(15);
                            arc_y += 3.6f;
                            count++;
                            postInvalidate();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        break;
                }
                if (count >= score)
                    break;
            }
        }
    }
}
